﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using Lvzhi.Core;
using Lvzhi.Entities;
using Webdiyer.WebControls.Mvc;

namespace Lvzhi.AdminCenter.Controllers
{
    public class GoodsController : BaseController
    {
        private GoodsCore gCore = new GoodsCore();
        private CategoryCore cCore = new CategoryCore();
        /// <summary>
        /// 宝贝列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(int id = 1, int size = 10, string key = "")
        {

            Expression<Func<Core_Goods, Boolean>> expr = t => true;
            if (!string.IsNullOrWhiteSpace(key))
            {
                expr = t => t.G_Name.Contains(key.Trim());
            }
            var list = gCore.LoadEntities(expr).OrderByDescending(o => o.G_Top).ThenByDescending(o => o.G_Time).ToPagedList(id, size);
            return View(list);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit(Guid? id)
        {
            Core_Goods info = null;
            if (id.HasValue)
            {
                info = gCore.LoadEntity(o => o.G_Id == id);
            }
            else
            {
                info = new Core_Goods();
            }
            //商品分类
            List<SelectListItem> cateList = new List<SelectListItem>();
            var clist = cCore.LoadEntities(o => o.C_State == 0).ToList();

            foreach (var item in clist.Where(o => o.C_PId == 0))
            {
                cateList.Add(new SelectListItem()
                {
                    Selected = item.C_Id == info.G_TypeId,
                    Text = item.C_Title,
                    Value = item.C_Id.ToString()
                });
                cateList.AddRange(clist.Where(o => o.C_PId == item.C_Id).Select(c => new SelectListItem()
                {
                    Selected = c.C_Id == info.G_TypeId,
                    Text = "　　∟" + c.C_Title, //┗
                    Value = c.C_Id.ToString()
                }));
            }

            //IEnumerable<SelectListItem> cList = cCore.LoadEntities(o => o.C_State == 0).Select(c => new SelectListItem()
            //{
            //    Selected = c.C_Id == info.G_TypeId,
            //    Text = c.C_Title,
            //    Value = c.C_Id.ToString()
            //});

            TempData["GoodsType"] = cateList;
            return View(info);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(FormCollection obj)
        {
            Core_Goods goods = new Core_Goods();
            TryUpdateModel(goods, obj);
            goods.G_Time = DateTime.Now;
            if (goods.G_Id == Guid.Empty)
            {
                goods.G_Id = Guid.NewGuid();
                gCore.AddEntity(goods);
            }
            else
            {
                gCore.UpdateEntity(goods);
            }
            //附件不为空
            if (!string.IsNullOrEmpty(obj["coverImgs"]))
            {
                AttachmentCore attCore = new AttachmentCore();

                if (goods.G_Id != Guid.Empty)
                {
                    attCore.DeleteByExtended(o => o.A_PId == goods.G_Id);  //先删了之前的附件
                }
                List<Sys_Attachment> alist = obj["coverImgs"].Split(',').Where(o => !string.IsNullOrEmpty(o)).Select((o, i) => new Sys_Attachment
                {
                    A_Id = Guid.NewGuid(),
                    A_CloudUlr = o.ToString(),
                    A_PId = goods.G_Id,
                    A_Type = 1,
                    A_Size = 0,
                    A_Time = DateTime.Now,
                    A_Rank = i  //排序

                }).ToList();

                attCore.AddEntities(alist);
            }

            return RedirectToAction("Index");
        }


        /// <summary>
        /// 下架商品
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPost]
        public string Forzen(Guid id, int status)
        {
            string rs = "ok";
            var obj = gCore.LoadEntity(o => o.G_Id == id);
            obj.G_Status = status;
            if (!gCore.UpdateEntity(obj))  //更新状态
            {
                rs = "error";
            }
            return rs;
        }

    }
}